package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.main.X3CarAdapter;
import com.das.mechanic_base.bean.ground.UserCarInfoBean;
import com.das.mechanic_base.utils.X3ScreenUtils;

import java.util.List;

public class X3SelectCarPopuWindow extends PopupWindow implements X3CarAdapter.IOnClick {

    private View containView;

    private RecyclerView rlv_car;

    private OnAdapterItemClick onAdapterItemClick;

    private Context mContext;
    private final X3CarAdapter carAdapter;

    public X3SelectCarPopuWindow(Context context) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout fView = new LinearLayout(context);
        containView = layoutInflater.inflate(R.layout.x3_select_car_pop_item, fView);
        rlv_car = containView.findViewById(R.id.rlv_car);

        containView.setOnClickListener(view -> dismiss());

        rlv_car.setLayoutManager(new LinearLayoutManager(context));
        carAdapter = new X3CarAdapter(context);
        rlv_car.setAdapter(carAdapter);
        carAdapter.setiOnClick(this);

        this.mContext = context;
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setBackgroundDrawable(null);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //this.setAnimationStyle(R.style.pop_shop_anim);
        setContentView(containView);
    }

    // 赋值
    public void setData(List<UserCarInfoBean> carInfoBeans){
        if (carAdapter != null) {
            carAdapter.changeData(carInfoBeans);
        }
    }

    // 刷新选中的数据
    public void setSelectData(String carNum) {
        if (carAdapter != null) {
            carAdapter.changeSelect(carNum);
        }
    }

    public void showPopu(View view) {
        if (!this.isShowing()) {
            this.showAsDropDown(view,0, X3ScreenUtils.dipToPx(20,mContext));
        }
    }

    public OnAdapterItemClick getmOnItemClick() {
        return onAdapterItemClick;
    }

    public void setmOnItemClick(OnAdapterItemClick mOnItemClick) {
        this.onAdapterItemClick = mOnItemClick;
    }

    @Override
    public void iOnClickCar(String carNum) {
        // 点击获取车牌号
        if (onAdapterItemClick != null) {
            onAdapterItemClick.onAdapterClick(carNum);
        }
        dismiss();
    }

    public interface OnAdapterItemClick {
        void onAdapterClick(String carNum);
    }
}
